<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">触手客户端</label>
        <el-input v-model="query.clientid" clearable placeholder="触手客户端" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">ip</label>
        <el-input v-model="query.ip" clearable placeholder="ip" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">描述</label>
        <el-input v-model="query.clientDesc" clearable placeholder="描述" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="520px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
          <el-form-item label="触手客户端" prop="clientid">
            <el-select
              v-model="form.clientid"
              remote
              reserve-keyword
              :loading="loading"
            >
              <el-option
                v-for="item in options"
                :key="item.clientid"
                :label="item.clientIp+item.clientDesc"
                :value="item.clientid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="ip" prop="ip">
            <el-input v-model="form.ip" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="apiurl" prop="apiurl">
            <el-input v-model="form.apiurl" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="userid" prop="userid">
            <el-input v-model="form.userid" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="apitoken" prop="apitoken">
            <el-input v-model="form.apitoken" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="描述" prop="clientDesc">
            <el-input v-model="form.clientDesc" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="clientid" label="触手客户端" />
        <el-table-column prop="ip" label="ip" />
        <el-table-column prop="apiurl" label="apiurl" />
        <el-table-column prop="userid" label="userid" />
        <el-table-column prop="apitoken" label="apitoken" />
        <el-table-column prop="moduser" label="修改人" />
        <el-table-column prop="modtime" label="修改时间" />
        <el-table-column prop="clientDesc" label="描述" />
        <el-table-column v-if="checkPer(['admin','devopsJenkinsclient:edit','devopsJenkinsclient:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDevopsJenkinsclient from '@/api/devops/devopsJenkinsClient'
import { getTrees } from '@/api/devops/devopsClient'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { jenkinsid: null, clientid: null, apiurl: null, ip: null, userid: null, apitoken: null, flag: null, notes: null, reguser: null, regtime: null, moduser: null, modtime: null, clientDesc: null }
export default {
  name: 'DevopsJenkinsclient',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'DEVOPS:jenkins客户端', url: 'api/devops/devopsJenkinsclient', idField: 'jenkinsid', sort: 'jenkinsid,desc', crudMethod: { ...crudDevopsJenkinsclient }})
  },
  data() {
    return {
      options: [],
      loading: false,
      permission: {
        add: ['admin', 'devopsJenkinsclient:add'],
        edit: ['admin', 'devopsJenkinsclient:edit'],
        del: ['admin', 'devopsJenkinsclient:del']
      },
      rules: {
        clientid: [
          { required: true, message: '触手客户端不能为空', trigger: 'blur' }
        ],
        ip: [
          { required: true, message: 'ip不能为空', trigger: 'blur' }
        ],
        apiurl: [
          { required: true, message: 'apiurl不能为空', trigger: 'blur' }
        ],
        userid: [
          { required: true, message: 'userid不能为空', trigger: 'blur' }
        ],
        apitoken: [
          { required: true, message: 'apitoken不能为空', trigger: 'blur' }
        ],
        clientDesc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'clientid', display_name: '触手客户端' },
        { key: 'ip', display_name: 'ip' },
        { key: 'clientDesc', display_name: '描述' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },

    [CRUD.HOOK.afterToCU](crud, form) {
      this.loading = false
      getTrees().then((res) => {
        this.loading = false
        this.options = res
        console.log(res)
      }).catch(() => {
        this.loading = false
      })
    }

  }
}
</script>

<style scoped>

</style>
